package cn.java83.zsb.springcloudserve.web;

import cn.java83.zsb.springcloudserve.entity.Advtise;
import cn.java83.zsb.springcloudserve.entity.Page;
import cn.java83.zsb.springcloudserve.entity.Result;
import cn.java83.zsb.springcloudserve.service.AdvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("adv")
public class AdvController {
    @Autowired
    private AdvService advService;

    @GetMapping("/list")
    public Page<Advtise> search(@RequestParam(name = "page", defaultValue = "1") int page,
                                @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                @RequestParam(name = "title", required = false) String title,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(name = "start", required =
                                        false) Date start,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(name = "end", required = false) Date end) {
        return advService.search(page, pageSize, title, start, end);
    }

    @PostMapping("add")
    public Result add(@RequestBody Advtise ad) {
        boolean r = advService.save(ad);
        return r ? Result.success() : Result.error("增加失败");
    }

    @GetMapping("get/{id}")
    public Result get(@PathVariable("id")Long id) {
        Advtise r = advService.getById(id);
        return r!=null ? Result.success(r) : Result.error("对象"+id+"没有找到！");
    }
}